cmake_minimum_required(VERSION 3.16)
include(CMakePrintHelpers)
project(nas_plugin_obj)
file(GLOB_RECURSE native_dtfuzz_srcs src/dt_fuzz/*.cpp)
MESSAGE("Compling the llt of file plugin.")
option(DTFUZZ "Compile in DTFUZZ mode" OFF)
add_definitions(-w)
add_definitions(-DBOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC)
# 定义_NAS编译宏
add_definitions(-D_NAS)
SET(LLT ON)

add_definitions(-DBOOST_ASIO_DISABLE_STD_ALIGNED_ALLOC)

# 定义一些公共变量
SET(PLUGIN_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
SET(PLUGIN_SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../src)
SET(PLUGIN_FRAMEWORK_PATH ${PLUGIN_ROOT_PATH}/framework)
SET(FRAMEWORK_INC_PATH ${PLUGIN_ROOT_PATH}/framework/inc)
SET(PLUGIN_COMMON_PATH ${PLUGIN_SRC_PATH}/common)
SET(PLUGIN_FRAMEWORK_LIB_PATH ${PLUGIN_FRAMEWORK_PATH}/lib)
SET(MODULE_ROOT_PATH ${PLUGIN_ROOT_PATH}/Module)
SET(SCANNER_ROOT_PATH ${PLUGIN_ROOT_PATH}/FS_Scanner)
SET(BACKUP_ROOT_PATH ${PLUGIN_ROOT_PATH}/FS_Backup)
SET(OPEN_SRC_PATH ${MODULE_ROOT_PATH}/third_open_src)
SET(PLATFORM_PATH ${MODULE_ROOT_PATH}/platform)
SET(OPENSRC_THRIFT_INC_PATH ${OPEN_SRC_PATH}/thrift_rel/include/)
set(AGENT_SDK_PATH ${PLUGIN_FRAMEWORK_PATH}/dep/agent_sdk)
SET(DT_UTILS_PATH ${MODULE_ROOT_PATH}/dt_utils)
SET(GOOGLE_TEST_PATH ${DT_UTILS_PATH}/gmock/googletest-release)
SET(MOCKCPP_PATH ${DT_UTILS_PATH}/mockcpp/build/mockcpp)

# 查看目标库是否存在
if(TARGET curl_http_util)
    MESSAGE(STATUS "curl_http_util existed")
else()
    add_subdirectory(${MODULE_ROOT_PATH}/src/curl_http curl_http)
endif()

if (DEFINED DTFUZZ)
        message(STATUS "DTFUZZ defined: " ${DTFUZZ})
else ()
        message(STATUS "DTFUZZ un-defined: " ${DTFUZZ})
endif()
if(DTFUZZ)
SET(SECODE_FUZZ_INCLUDE_PATH ${DT_UTILS_PATH}/secodefuzz_rel/include)
SET(SECODE_FUZZ_LIB_PATH ${DT_UTILS_PATH}/secodefuzz_rel/lib)
SET(SECODE_FUZZ_LIB_NAME Secodefuzz)
set(nas_test_src
    src/NasTest.cpp
    ${native_dtfuzz_srcs}
)
add_executable(test_nas_plugin
    ${nas_test_src}
)
set(FUZZ_OPT "-fdump-rtl-expand -fsanitize-coverage=trace-pc")
add_definitions(-DFUZZ_ENABLED)
else()

aux_source_directory(src nasTest)
aux_source_directory(src/common/application applicationTest)
aux_source_directory(src/common/application ApplicationManagerTest)
aux_source_directory(src/common/constant constantTest)
aux_source_directory(src/common/constant JsonTransUtilTest)
aux_source_directory(src/component/statistics statisticsTest)
aux_source_directory(src/component/statistics ShareResourceManagerTest)
aux_source_directory(src/job jobTest)
aux_source_directory(src/nas_service/nas_archive nas_archiveTest)
aux_source_directory(src/nas_service/nas_index nas_indexTest)
aux_source_directory(src/nas_service/nas_livemount nas_livemountTest)
aux_source_directory(src/nas_service/nas_replication nas_replicationTest)
aux_source_directory(src/nas_service/nas_replication/backup nas_replicationBackupTest)
aux_source_directory(src/nas_service/nas_replication/replication nas_replicationReplicationTest)
aux_source_directory(src/nas_service/nas_replication/restore nas_replicationRestoreTest)
aux_source_directory(src/nas_service/nas_share/backup nas_shareBackupTest)
aux_source_directory(src/nas_service/nas_share/common nas_shareCommonTest)
aux_source_directory(src/nas_service/nas_share/restore nas_shareRestoreTest)
aux_source_directory(src/nas_service/nas_snapshot nas_snapshotTest)
aux_source_directory(src/nas_service/nas_snapshot/backup nas_snapshotBackupTest)
aux_source_directory(src/nas_service/nas_snapshot/delete nas_snapshotDeleteTest)
aux_source_directory(src/nas_service/nas_snapshot/restore nas_snapshotRestoreTest)
aux_source_directory(src/nas_service/nas_resource nas_resourceAppServiceTest)
aux_source_directory(src/utils utilsTest)

add_executable(test_nas_plugin
    ${nasTest}
    ${applicationTest}
    ${ApplicationManagerTest}
    ${constantTest}
    ${JsonTransUtilTest}
    ${statisticsTest}
    ${ShareResourceManagerTest}
    ${jobTest}
    ${nas_archiveTest}
    ${nas_indexTest}
    ${nas_livemountTest}
    ${nas_replicationTest}
    ${nas_replicationBackupTest}
    ${nas_replicationReplicationTest}
    ${nas_replicationRestoreTest}
    ${nas_shareBackupTest}
    ${nas_shareCommonTest}
    ${nas_shareRestoreTest}
    ${nas_snapshotTest}
    ${nas_snapshotBackupTest}
    ${nas_snapshotDeleteTest}
    ${nas_snapshotRestoreTest}
    ${nas_resourceAppServiceTest}
    ${utilsTest}
)
endif()

add_subdirectory(../src nas_src)

set(NAS_PLUGIN_INC_FOLDER
    ${MODULE_ROOT_PATH}/dt_utils/llt_stub

    # 业务代码依赖
    ${PLUGIN_ROOT_PATH}
    ${PLUGIN_SRC_PATH}
    ${PLUGIN_SRC_PATH}/common
    ${PLUGIN_SRC_PATH}/common/application
    ${PLUGIN_SRC_PATH}/common/constant
    ${PLUGIN_SRC_PATH}/component
    ${PLUGIN_SRC_PATH}/component/statistics
    ${PLUGIN_SRC_PATH}/job
    ${PLUGIN_SRC_PATH}/nas_service
    ${PLUGIN_SRC_PATH}/nas_service/nas_archive
    ${PLUGIN_SRC_PATH}/nas_service/nas_index
    ${PLUGIN_SRC_PATH}/nas_service/nas_livemount
    ${PLUGIN_SRC_PATH}/nas_service/nas_replication
    ${PLUGIN_SRC_PATH}/nas_service/nas_replication/backup
    ${PLUGIN_SRC_PATH}/nas_service/nas_replication/replication
    ${PLUGIN_SRC_PATH}/nas_service/nas_replication/restore
    ${PLUGIN_SRC_PATH}/nas_service/nas_share
    ${PLUGIN_SRC_PATH}/nas_service/nas_share/backup
    ${PLUGIN_SRC_PATH}/nas_service/nas_share/common
    ${PLUGIN_SRC_PATH}/nas_service/nas_share/restore
    ${PLUGIN_SRC_PATH}/nas_service/nas_snapshot
    ${PLUGIN_SRC_PATH}/nas_service/nas_snapshot/backup
    ${PLUGIN_SRC_PATH}/nas_service/nas_snapshot/delete
    ${PLUGIN_SRC_PATH}/nas_service/nas_snapshot/restore
    ${PLUGIN_SRC_PATH}/nas_service/nas_resource
    ${PLUGIN_SRC_PATH}/utils

    # 开源依赖
    ${PLATFORM_PATH}/SecureCLib_rel/include
    ${PLATFORM_PATH}/KMCv3_infra_rel/include
    ${PLATFORM_PATH}/KMCV3_rel/include
    ${OPEN_SRC_PATH}/boost_rel/include
    ${OPEN_SRC_PATH}/openssl_rel/include
    ${OPEN_SRC_PATH}/libxml2_rel/include
    ${OPEN_SRC_PATH}/jsoncpp_rel/include
    ${OPEN_SRC_PATH}/esdk_rel/include

    # framework依赖
    ${PLUGIN_FRAMEWORK_PATH}/dep/agent_sdk/include
    ${FRAMEWORK_INC_PATH}
    ${FRAMEWORK_INC_PATH}/job
    ${FRAMEWORK_INC_PATH}/job_mgr
    ${FRAMEWORK_INC_PATH}/client
    ${FRAMEWORK_INC_PATH}/common
    ${FRAMEWORK_INC_PATH}/thrift_interface
    ${FRAMEWORK_INC_PATH}/rpc
    ${FRAMEWORK_INC_PATH}/rpc/certificateservice
    ${OPENSRC_THRIFT_INC_PATH}

    # Module依赖
    ${MODULE_ROOT_PATH}
    ${MODULE_ROOT_PATH}/src
    ${MODULE_ROOT_PATH}/src/parser
    ${MODULE_ROOT_PATH}/src/common
    ${MODULE_ROOT_PATH}/src/libnfs_ctx
    ${MODULE_ROOT_PATH}/src/threadpool
    ${MODULE_ROOT_PATH}/third_open_src/lnfs_rel/include
    ${MODULE_ROOT_PATH}/third_open_src/lsmb2_rel/include
    ${MODULE_ROOT_PATH}/third_open_src/boost_rel/lib
    ${MODULE_ROOT_PATH}/src/libsmb_ctx

    # Backup依赖
    ${BACKUP_ROOT_PATH}/src/model
    ${BACKUP_ROOT_PATH}/src/service
    ${BACKUP_ROOT_PATH}/src/common
    ${BACKUP_ROOT_PATH}/src/engines
    ${BACKUP_ROOT_PATH}/src/engines/posix
    ${BACKUP_ROOT_PATH}/src/service/copy
    ${BACKUP_ROOT_PATH}/src/service/delete
    ${BACKUP_ROOT_PATH}/src/service/hardlink
    ${BACKUP_ROOT_PATH}/src/service/dir

    # Scanner依赖
    ${SCANNER_ROOT_PATH}/localhost_src
    ${SCANNER_ROOT_PATH}/localhost_src/filter

    # gtest
    ${GOOGLE_TEST_PATH}/googletest/include
    ${GOOGLE_TEST_PATH}/googlemock/include
    ${SECODE_FUZZ_INCLUDE_PATH}

    # mockcpp
    ${MOCKCPP_PATH}/include
)

target_include_directories(test_nas_plugin
    PRIVATE
    ${NAS_PLUGIN_INC_FOLDER}
)

set(NAS_PLUGIN_LIB_FOLDER
    ${PLATFORM_PATH}/SecureCLib_rel/lib
    ${PLATFORM_PATH}/KMCv3_infra_rel/lib
    ${PLATFORM_PATH}/KMCV3_rel/lib
    ${OPEN_SRC_PATH}/jsoncpp_rel/libs
    ${OPEN_SRC_PATH}/boost_rel/lib
    ${OPEN_SRC_PATH}/esdk_rel/lib
    ${OPEN_SRC_PATH}/curl_rel/lib
    ${OPEN_SRC_PATH}/lnfs_rel/lib
    ${OPEN_SRC_PATH}/lsmb2_rel/lib
    ${OPEN_SRC_PATH}/leveldb_rel/out-shared
    ${OPEN_SRC_PATH}/protobuf_rel/lib
    ${OPEN_SRC_PATH}/sqlite_rel/unixODBC/lib
    ${OPEN_SRC_PATH}/LibRdKafka_rel/libs
    ${OPEN_SRC_PATH}/libssh2_rel/lib
    ${OPEN_SRC_PATH}/openssl_rel/lib
    ${OPEN_SRC_PATH}/soci_rel/lib64
    ${OPEN_SRC_PATH}/libiconv_rel/lib
    ${OPEN_SRC_PATH}/openssl_rel/lib
    ${OPEN_SRC_PATH}/libevent_rel/lib
    ${PLUGIN_FRAMEWORK_PATH}/lib
    ${PLUGIN_FRAMEWORK_PATH}/lib/dme
    ${PLUGIN_FRAMEWORK_PATH}/lib/3rd
    ${PLUGIN_FRAMEWORK_PATH}/lib/agent_sdk
    ${PLUGIN_FRAMEWORK_PATH}/lib/dme/3rd
    ${DME_OPENSRC_CARES_LIB_PATH}
    ${SECODE_FUZZ_LIB_PATH}
)

target_link_directories(test_nas_plugin
    PRIVATE
    ${NAS_PLUGIN_LIB_FOLDER}
    ${AGENT_SDK_PATH}/lib
    ${CMAKE_CURRENT_SOURCE_DIR}/../build-cmake
    ${CMAKE_CURRENT_SOURCE_DIR}/../build-cmake/system
    ${CMAKE_CURRENT_SOURCE_DIR}/../build-cmake/io_device
    ${CMAKE_CURRENT_SOURCE_DIR}/../build-cmake/device_access
    ${BACKUP_ROOT_PATH}/build-cmake
    ${SCANNER_ROOT_PATH}/build-cmake-file
    ${GOOGLE_TEST_PATH}/lib
    ${MODULE_ROOT_PATH}/lib
    # mockcpp
    ${MOCKCPP_PATH}/lib
)

set(KMC_OPT "-DWSEC_COMPILE_CAC_OPENSSL -DWSEC_USE_OPENSSL_110 -DWSEC_LINUX -DKMC_MULTIPROCESS -DWSEC_ERR_CODE_BASE=0x10000000")

target_compile_options(test_nas_plugin
    PRIVATE
    -std=c++1z -fno-access-control -DBOOST_LOG_DYN_LINK
    -g -pipe -fPIC -DLINUX ${KMC_OPT}
    -fprofile-arcs -ftest-coverage
    -Wl,-Bstatic -Wl,-Bdynamic
)

target_link_libraries(test_nas_plugin
    basic_job
    curl_http_util
    scanner_interface
    pthread
    nas_plugin
    pluginsdk
    Scanner
    securec
    json
    nfs
    smb2
    boost_chrono
    boost_atomic
    boost_thread
    boost_filesystem
    boost_system
    boost_regex
    boost_log
    boost_date_time
    eSDKOBS
    eSDKLogAPI
    iconv
    ssl
    crypto
    thrift_interface
    thrift_client
    rpc
    common_util
    config_reader_util
    log_util
    system
    io_device
    device_access
    kmcv3
    KMC
    SDP
    gmock gtest gtest_main gmock_main gcov
    mockcpp
    ${SECODE_FUZZ_LIB_NAME}
    # Module依赖
    nfs_ctx
    smb_ctx
    parser
    threadpool
    system
    Backup
)
